<?php
/**
 * 教职工
 *
 *
 */
class Users_model extends Model
{

    var $uid;

	var $username;
	
	var $college_id;
	
	var $name;
	
	var $age;
	
	var $department_id;
	
	var $last_login_at;
	
	var $last_login_ip;
    	
	function __construct()
    {
        parent::Model();
    }

	// --------------------------------------------------------------------

    /**
	 * load by id
	 *
	 *
	 */	
    function load($id)
    {
        if (!$id){
            return array();
        }

        $query = $this->db->get_where('users',array('uid' => $id));

        if ($row = $query->row_array()){
            return $row;
        }

        return array();
    }

	// --------------------------------------------------------------------

    /**
	 * 创建
	 *
	 *
	 */	
    function create()
    { 
		 
        $this->db->set('uid', $this->uid);
		$this->db->set('username', $this->username);
        $this->db->set('college_id', $this->college_id);
		$this->db->set('name', $this->name); 
        $this->db->set('age', $this->age);
		$this->db->set('department_id', $this->department_id);             
		$this->db->set('last_login_at', $this->last_login_at); 
        $this->db->set('last_login_ip', $this->last_login_ip);
 
        return $this->db->insert('users');
    }

	// --------------------------------------------------------------------

    /**
	 * 结果集
	 *
	 *
	 */	
    function get_all($sort_field,$sort_asc='asc')
	{
        if (!empty($sort_field)){
			$this->db->order_by($sort_field,$sort_asc);
		}
		$query = $this->db->get('users');
        $rows = array();
        foreach ($query->result_array() as $row){
            $rows[$row['uid']] = $row;
        }
        return $rows;
	}

    // --------------------------------------------------------------------

    /**
	 * 更新
	 *
	 *
	 */	
    function update($id)
    {
         
 

        $this->db->set('college_id', $this->college_id);
		$this->db->set('name', $this->name); 
        $this->db->set('age', $this->age);
		$this->db->set('department_id', $this->department_id);             
		$this->db->set('last_login_at', $this->last_login_at); 
        $this->db->set('last_login_ip', $this->last_login_ip);
 
        $this->db->where('uid', $id);
        return $this->db->update('users');
    }
    
 

    // --------------------------------------------------------------------

    /**
	 * 删除
	 *
	 *
	 */	
    function delete($id)
    {        
		$this->db->where('uid', $id);

        return $this->db->delete('users'); 
    }

   /**
    * 属于教研室的用户数
    */
    function  user_department_exits($department_id){
    	
    	
    	$this->db->where('department_id',$department_id);
    	$query = $this->db->get('users');
    	return  $query->num_rows();
    }
    
    
    /**
     * 属于学院的用户数
     * 
     */
     function  user_college_exits($id){
    	
    	
    	$this->db->where('college_id',$id);
    	$query = $this->db->get('users');
    	return  $query->num_rows();
    }   

}